package cn.hsa.xxx.bookOrder.bo.impl;


import cn.hsa.xxx.bookOrder.bo.OrderDetailsBO;
import cn.hsa.xxx.bookOrder.dao.OrderDetailsDAO;
import cn.hsa.xxx.bookOrder.dto.OrderDetailsDTO;
import cn.hsa.xxx.bookOrder.dto.OrderDetailsDTOConverter;
import cn.hsa.xxx.bookOrder.entity.OrderDetailsDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class OrderDetailsBOImpl implements OrderDetailsBO {
    @Resource
    OrderDetailsDAO orderDetailsDAO;

    @Autowired
    OrderDetailsDTOConverter orderDetailsDTOConverter;

    @Override
    public OrderDetailsDTO add(OrderDetailsDTO orderDetailsDTO) {
        OrderDetailsDO orderDetailsDO = orderDetailsDTOConverter.convertToOrderDetailsDO(orderDetailsDTO);
        orderDetailsDAO.insert(orderDetailsDO);
        return orderDetailsDTOConverter.convertToOrderDetailsDTO(orderDetailsDO);
    }

    @Override
    public String delete(String id) {
        orderDetailsDAO.deleteById(id);
        return "删除成功";
    }

    @Override
    public OrderDetailsDTO get(String id) {
        OrderDetailsDO orderDetailsDO = orderDetailsDAO.selectById(id);
        OrderDetailsDTO orderDetailsDTO = orderDetailsDTOConverter.convertToOrderDetailsDTO(orderDetailsDO);
        return orderDetailsDTO;
    }

    @Override
    public String update(OrderDetailsDTO orderDetailsDTO) {
        OrderDetailsDO orderDetailsDO = orderDetailsDTOConverter.convertToOrderDetailsDO(orderDetailsDTO);
        orderDetailsDAO.updateById(orderDetailsDO);
        return "更新成功";
    }

    @Override
    public List<OrderDetailsDTO> getPage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<OrderDetailsDO> orderDetailsDOList = orderDetailsDAO.selectList(null);

        PageInfo pageInfo = new PageInfo(orderDetailsDOList);
        long totalRecords = pageInfo.getTotal();
        long totalPages = pageInfo.getPages();

        List<OrderDetailsDTO> orderDetailsDTOList = new ArrayList<>();
        for (OrderDetailsDO orderDetailsDO : orderDetailsDOList) {
            orderDetailsDTOList.add(orderDetailsDTOConverter.convertToOrderDetailsDTO(orderDetailsDO));
        }

        return orderDetailsDTOList;
    }
}
